package com.elsa.smart_buffet.controller;

import com.alibaba.fastjson.JSON;
import com.elsa.smart_buffet.context.UserContext;
import com.elsa.smart_buffet.pojo.MenuOrder;
import com.elsa.smart_buffet.pojo.Order;
import com.elsa.smart_buffet.pojo.ResultBox.ResponseResult;
import com.elsa.smart_buffet.service.OrderService;
import com.elsa.smart_buffet.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/order/list")
    public ResponseResult insertOrder(@RequestBody List<Map<String, Object>> list) throws InterruptedException {
        log.info(String.valueOf(list));
        return orderService.insertOrder(list);
    }

    @PostMapping("/order/data/list")
    public ResponseResult insertDataOrder(@RequestBody String data) throws InterruptedException {
        List<Map<String, Object>> list = (List<Map<String, Object>>) JSON.parseObject(data).get("data");
        log.info(String.valueOf(list));
        return orderService.insertOrder(list);
    }

    @PostMapping("/order")
    public ResponseResult insertOrder(@RequestBody MenuOrder menuOrder) {
        log.info(UserContext.currentUserName());
        return orderService.insertOrder(menuOrder);
    }

    @PostMapping("/selectOrderByDate")
    public ResponseResult selectOrderByDate(@RequestBody String time) {
        log.info("selectOrderByDate接受到的时间字符串" + JSON.parseObject(time).get("time").toString());
        String[] time1 = JSON.parseObject(time).get("time").toString().split("\\-");
        for(String time2 : time1) {
            // 将前端传的年月日字符串转成无‘-’的形式
            time2 = time2.replace("/", "-");
            log.info("selectOrderByDate接受到的时间字符串" + time2);
            Date date = DateUtil.StringToDate(time2);
        }
        return orderService.selectOrderByDate(JSON.parseObject(time).get("time").toString());
    }

    @GetMapping("/order/list")
    public ResponseResult selectAllOrder() {
        log.info(UserContext.currentUserName());
        return orderService.selectAllOrderInOrderById();
    }

    @GetMapping("/order/{name}")
    public ResponseResult selectOrderByConsumerName(@PathVariable("name") String name) {
        log.info(UserContext.currentUserName());
        return orderService.selectOrderByConsumerName(name);
    }

    @GetMapping("/order/one/{oId}")
    public ResponseResult selectOrderByOId(@PathVariable("oId") String oId) {
        log.info(UserContext.currentUserName());
        return orderService.selectOrderByOId(oId);
    }

    @PostMapping("/selectOrderByCId")
    public ResponseResult selectOrderByCId(@RequestBody String CId) {
        return orderService.selectOrderByCId(JSON.parseObject(CId).get("CId").toString());
    }

    @PostMapping("/selectOrderByOIdWithMenu")
    public ResponseResult selectOrderByOIdWithMenu(@RequestBody String OId) {
        return orderService.selectOrderByOIdWithMenu(JSON.parseObject(OId).get("OId").toString());
    }

}
